package com.hustcad.plm.pdm.user.controller;



import cn.hutool.core.lang.UUID;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.user.service.OptionService;
import com.jsyxdl.k3cloud.listener.K3CloudHttpService;
import com.ty.basic.response.ResponseResult;
import liquibase.pro.packaged.J;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/v1/option")
public class OptionController {

    @Autowired
    OptionService optionService;

    public OptionController(OptionService optionService) {
        this.optionService = optionService;
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Map<String,Object> map) {
        optionService.addOption(map);
        return ResponseResult.success();
    }

    @RequestMapping("/list")
    public ResponseResult<PageInfo<Map<String,Object>>> list(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list = optionService.list(map);
        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }
    @RequestMapping("/edit")
    public ResponseResult edit(@RequestBody Map<String,Object> map) {
        optionService.editOption(map);
        return ResponseResult.success();
    }
    @RequestMapping("/delete")
    public ResponseResult delete(@RequestBody Map<String,Object> map) {
        optionService.delete(map);
        return ResponseResult.success();
    }
    @RequestMapping("/addEnumType")
    public ResponseResult addEnumType(@RequestBody Map<String,Object> map) {
        String uuid=UUID.randomUUID().toString();
        map.put("id",uuid);
        List<Map<String,Object>> list= (List<Map<String, Object>>) map.get("list");
        for(int i=0;i<list.size();i++){
            list.get(i).put("enum_id",uuid);
            list.get(i).put("id",UUID.randomUUID().toString());

        }
        optionService.addEnumType(map,list);
        return ResponseResult.success();
    }
    @RequestMapping("/updateEnumType")
    public ResponseResult updateEnumType(@RequestBody Map<String,Object> map) {

        List<Map<String,Object>> list= (List<Map<String, Object>>) map.get("list");
        for(int i=0;i<list.size();i++){
            list.get(i).put("enum_id",map.get("id"));
        }
        optionService.updateEnumType(map,list);
        return ResponseResult.success();
    }
    @RequestMapping("/deleteEnumType")
    public ResponseResult deleteEnumType(@RequestBody Map<String,Object> map) {
        List<Map<String,Object>> list= (List<Map<String, Object>>) map.get("list");
        optionService.deleteEnumType(map,list);
        return ResponseResult.success();
    }
    @RequestMapping("/getEnumType")
    public ResponseResult<PageInfo<Map<String,Object>>> getEnumType(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list=optionService.getEnum(map);
        if(map.containsKey("id")==false || "".equals(map.get("id"))){

        }
        else{
            List<Map<String,Object>> sList=optionService.getEnumSelect(map);
            for(int i=0;i<list.size();i++){
                list.get(i).put("list",sList);
            }

        }

        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }

    @RequestMapping("/addOutSettingEnum")
    public ResponseResult addOutSettingEnum(@RequestBody Map<String,Object> map) {
        map.put("id",UUID.randomUUID().toString());
        optionService.addOutSettingEnum(map);
        return ResponseResult.success();
    }
    @RequestMapping("/getOutEnumSelect")
    public ResponseResult getOutEnumSelect(@RequestBody Map<String,Object> map) {
        List<Map<String,Object>> list= optionService.getOutEnumSelect(map);
        return ResponseResult.success(list);
    }
    @RequestMapping("/editOutPlmPartById")
    public ResponseResult editOutPlmPartById(@RequestBody Map<String,Object> map) {
        optionService.editOutPlmPartById(map);
        return ResponseResult.success();
    }

    @RequestMapping("/addInSettingEnum")
    public ResponseResult addInSettingEnum(@RequestBody Map<String,Object> map) {
        map.put("id",UUID.randomUUID().toString());
        optionService.addInSettingEnum(map);
        return ResponseResult.success();
    }
    @RequestMapping("/getInEnumSelect")
    public ResponseResult getInEnumSelect(@RequestBody Map<String,Object> map) {
        List<Map<String,Object>> list= optionService.getInEnumSelect(map);
        return ResponseResult.success(list);
    }
    @RequestMapping("/buildOutSetting")
    public ResponseResult buildOutSetting(@RequestBody Map<String,Object> map) {
        JSONObject obj=JSONObject.parseObject(JSONObject.toJSONString(map));
        FlowObject flowObject=obj.toJavaObject(FlowObject.class);
//        FlowObject flowObject=new FlowObject();
        flowObject.setRefObjectOid(BigInteger.valueOf(Long.parseLong((String) map.get("oid"))));
        flowObject.setRefObjectOtype((String) map.get("otype"));
//        K3CloudHttpService k3CloudHttpService= SpringUtil.getBean(K3CloudHttpService.class);
//        if(k3CloudHttpService!=null){
//            try {
//                Map<String,Object> result=k3CloudHttpService.plmSelectK3CloudOut((String) map.get("code"), (String) map.get("label"),flowObject);
//                return ResponseResult.success(result);
//            }catch (Exception e){
//                e.printStackTrace();
//                throw new RuntimeException(e.getMessage());
//            }
//        }
        return ResponseResult.success();
    }
}
